package com.clx.mx.pmml.common;

import java.nio.charset.Charset;

public class Base64 {
    private static final Charset DEAFAULT_CHARSET = Charset.forName("utf-8");

    private static final char[] Base64Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
            'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
            '4', '5', '6', '7', '8', '9', '#', '@'};

    private static final byte[] Base64Reversal = {-1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, 62, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 52, 53,
            54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, 63, 0, 1, 2, 3,
            4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
            25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
            35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};

    private static final char Bit4Symbol = '-';

    private static final char Bit8Symbol = '_';

    public static String encode(byte[] bytes) {
        int sourceLength = bytes.length;
        int groupLength = sourceLength / 3;
        int remainder = sourceLength % 3;
        int resultLength = groupLength * 4;
        if (remainder == 1) {
            resultLength += 3;
        } else if (remainder == 2) {
            resultLength += 4;
        }

        char[] alphabet = Base64Alphabet;

        char[] result = new char[resultLength];

        int sourceCursor = 0;
        int targetCursor = 0;
        for (int i = 0; i < groupLength; i++) {
            int byte1 = bytes[sourceCursor++] & 0xff;
            int byte2 = bytes[sourceCursor++] & 0xff;
            int byte3 = bytes[sourceCursor++] & 0xff;
            result[targetCursor++] = alphabet[(byte1 >> 2 & 0x3f)];
            result[targetCursor++] = alphabet[((((byte1 & 0x3) << 4) | (byte2 >> 4))) & 0x3f];
            result[targetCursor++] = alphabet[((((byte2 & 0xf) << 2) | (byte3 >> 6))) & 0x3f];
            result[targetCursor++] = alphabet[byte3 & 0x3f];
        }

        if (remainder > 0) {
            byte byte1 = bytes[sourceCursor++];
            result[targetCursor++] = alphabet[byte1 >> 2 & 0x3f];
            if (remainder == 1) {
                result[targetCursor++] = alphabet[((byte1 & 0x3) << 4) & 0x3f];
                result[targetCursor++] = Bit8Symbol;
            } else {
                byte byte2 = bytes[sourceCursor++];
                result[targetCursor++] = alphabet[(((byte1 & 0x3) << 4) | (byte2 >> 4)) & 0x3f];
                result[targetCursor++] = alphabet[((byte2 & 0xf) << 2)];
                result[targetCursor++] = Bit4Symbol;
            }
        }

        return new String(result);
    }

    public static String decode(String str) {
        char[] source = str.toCharArray();
        int strLength = source.length;
        int groupLength = 0;
        int remainder = 0;
        char last = source[strLength - 1];
        if (last == Bit8Symbol) {
            groupLength = (strLength - 3) / 4;
            remainder = 1;
        } else if (last == Bit4Symbol) {
            groupLength = strLength / 4 - 1;
            remainder = 2;
        } else {
            groupLength = strLength / 4;
        }

        byte[] reversal = Base64Reversal;

        byte[] result = new byte[groupLength * 3 + remainder];

        int sourceCursor = 0;
        int targetCursor = 0;
        for (int i = 0; i < groupLength; i++) {
            byte sbyte1 = reversal[source[sourceCursor++]];
            byte sbyte2 = reversal[source[sourceCursor++]];
            byte sbyte3 = reversal[source[sourceCursor++]];
            byte sbyte4 = reversal[source[sourceCursor++]];
            result[targetCursor++] = (byte) ((sbyte1 << 2) | (sbyte2 >> 4));
            result[targetCursor++] = (byte) ((sbyte2 << 4) | (sbyte3 >> 2));
            result[targetCursor++] = (byte) (((sbyte3 & 0x3) << 6) | sbyte4);
        }

        if (remainder > 0) {
            byte sbyte1 = reversal[source[sourceCursor++]];
            byte sbyte2 = reversal[source[sourceCursor++]];
            result[targetCursor++] = (byte) ((sbyte1 << 2) | (sbyte2 >> 4));
            if (remainder == 2) {
                byte sbyte3 = reversal[source[sourceCursor++]];
                result[targetCursor++] = (byte) ((sbyte2 << 4) | (sbyte3 >> 2));
            }
        }

        String s = new String(result, DEAFAULT_CHARSET);
        return s;
    }
}
